﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class PartDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Part.GetAll);
		}

		public static DataTable GetOne(string partINo)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@PartNo", SqlDbType.Int, partINo)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Part.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

        public static Part LoadOne(string partNo)
		{
			Part o = new Part();
            DataTable dt = GetOne(partNo);
			if(dt.Rows.Count > 0)
			{
                o.PartNo = dt.Rows[0][ToolConstants.Entities.Part.FieldName.PartNo].ToString();
                o.PartVietNamName = dt.Rows[0][ToolConstants.Entities.Part.FieldName.PartVietNamName].ToString();
			}

			return o;
		}


		public static int Insert(Part o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@PartNo", SqlDbType.Int, o.PartNo),
                        CreateParameter("@PartVietNamName", SqlDbType.NVarChar, o.PartVietNamName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Part.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(Part o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@PartNo", SqlDbType.Int, o.PartNo),
                        CreateParameter("@PartVietNamName", SqlDbType.NVarChar, o.PartVietNamName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Part.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(Part o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@PartNo", SqlDbType.Int, o.PartNo)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Part.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("222_Part");
            table.Columns.Add("PartNo");
			table.Columns.Add("PartName");

			return table;
		}

		#endregion
	}
}
